let keyword = getSearchArg('keyword');
$('#search_input').val(keyword);
let data = {
    page: 1,
    pagesize: 9,
    keyword
}

window.onload = async function () {
    getList(data);
}
async function getList({
    page = 1,
    pagesize = 9,
    keyword
}) {
    let data = {
        page,
        pagesize,
        keyword
    };

    let result = await sendAjax({
        data,
        url: '/v1/search',
        type: 'post'
    });
    if (result.code == 200) {
        let {
            page,
            pagesize,
            totalNum,
        } = result.data
        console.log(result.data);
        let str = '';
        for (let item of result.data.result) {
            str += `  <li><a href="video_detail.html?cid=${item.cid}">
            <div class="top">
                <img src="${item.image_src}" alt="" class="m">
                <p>学科：${item.subject_name}</p>
            </div>
            <div class="bottom">
                <div class="left">
                    <span>${item.type_name}${item.subject_name}第一节课程内容讲解</span>
                    <span class="time">地区：${item.area_name}</span>
                </div>
                <div class="right">${item.price==0?'免费学习':item.price+'元'}</div>
            </div>
            </a>
        </li>`
        }

        $('#lesson').html(str);

        // 获取分页器实例对象
        var myPager = $('#myPager').data('zui.pager');

        // 设置当前页码，并同时设置记录总数0， 每页记录数目
        myPager.set({
            page: Number(page),
            recTotal: totalNum,
            recPerPage: Number(pagesize)
        });
        let a =
            `搜索到 <span>${data.keyword}</span> 关键字共<span>${totalNum}</span> 条，共<span>${  Math.ceil(totalNum/Number(pagesize) )}</span>页`;
        $('#search_keyword').html(a);
    }
}
//点击搜索按钮搜索当前页面的内容并更改数据
$('#search').on('click', () => {
    data.keyword = $('#search_input').val();
    getList(data.keyword);
})

//分页器监听改变页面数据
$('#myPager').on('onPageChange', function (e, state, oldState) {
    data.page = state.page;
    getList(data)
});